package com.ccc.boot.generator.service;

import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.ccc.boot.generator.dto.SettingEntity;
import com.ccc.boot.generator.util.GenerateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 *
 *
 * @author superC
 * @date 2022/3/29
 * @version 0.1.0
 */

@Service
public class MyGeneratorService {

    /**
     * JDBC 驱动程序
     */
    @Value("${spring.datasource.driver-class-name}")
    private String driver;

    /**
     * JDBC 连接地址
     */
    @Value("${spring.datasource.url}")
    private String url;

    /**
     * 数据库账号
     */
    @Value("${spring.datasource.username}")
    private String username;

    /**
     *  数据库密码
     */
    @Value("${spring.datasource.password}")
    private String password;


    /**
     * 生成代码，
     * 该接口生成的代码文件路径为用户的当前工作目录
     *
     * @date 2022/3/30 13:46
     * @param settingEntity
     * @author superC
     */
    public void generate(SettingEntity settingEntity) {
        GenerateUtils.generateCode(settingEntity, dataSourceConfig());
    }

    /**
     * 生成代码，
     * 该接口生成的代码文件路径为默认路径D盘根路径
     *
     * @date 2022/3/30 13:46
     * @param settingEntity
     * @author superC
     */
    public void generateToDefaultDir(SettingEntity settingEntity) {
        GenerateUtils.generateCode("", settingEntity, dataSourceConfig());
    }

    /**
     * 数据源配置
     *
     * @return {@link DataSourceConfig}
     */
    private DataSourceConfig dataSourceConfig() {
        DataSourceConfig config = new DataSourceConfig();
        config.setDriverName(driver);
        config.setUrl(url);
        config.setUsername(username);
        config.setPassword(password);
        return config;
    }

}
